package de.fhtrier.gdw2.ships.menu;

import de.fhtrier.gdw2.ships.SpaceshipGame;
import de.fhtrier.gdw2.ships.menu.base.Button;
import de.fhtrier.gdw2.ships.menu.base.IActionListener;
import de.fhtrier.gdw2.ships.menu.base.Widget;

import java.util.ArrayList;
import java.util.List;

import org.newdawn.slick.Color;
import org.newdawn.slick.Font;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.state.StateBasedGame;

/**
 * Menu page
 * 
 * @author Lusito
 */
public class MenuPage {
	protected Widget focus;
	protected List<Widget> widgets = new ArrayList<>();
	protected MenuPage parent;
	protected GameContainer container;
	protected SpaceshipGame game;
	private String type;
	MenuManager menuManager;
	Color hoverColor = new Color(Integer.parseInt("F8BB08", 16));
	
	
	public MenuPage(GameContainer container, StateBasedGame game, MenuManager menuManager, MenuPage parent, String type) throws SlickException {
		this.container = container;
        
		if (!(game instanceof SpaceshipGame))
			throw new IllegalArgumentException("game is not of type AbstractGameBase");
		
		this.game = (SpaceshipGame)game;
		this.menuManager = menuManager;
		this.parent = parent;
		this.setType(type);
	}
	
	public void addWidget(Widget w) {
		widgets.add(w);
	}
    
	public void render(Graphics g) {
		for(Widget w: widgets)
			w.render(g);
	}
	
	public void close() {
		menuManager.setPage(parent);
	}
	

	public void addCenteredButton(final String text, float x, float y, Font font, IActionListener listener) throws SlickException {
		float w = font.getWidth(text);
		float h = font.getLineHeight();

		Button button = Button.create(text, x - w/2, y - h/2, w, h)
			.font(font)
			.color(Color.white)
			.hoverColor(hoverColor)
			.pressedColor(Color.cyan)
			.action(listener)
			.useTextSize(true);
		addWidget(button);
		button.init(container);
	}
	
	public void addLeftAlignedButton(final String text, float x, float y, Font font, IActionListener listener) throws SlickException {
		float w = font.getWidth(text);
		float h = font.getLineHeight();

		Button button = Button.create(text, x, y, w, h)
			.font(font)
			.color(Color.white)
			.hoverColor(hoverColor)
			.pressedColor(Color.cyan)
			.action(listener)
			.useTextSize(true);
		addWidget(button);
		button.init(container);
	}

	
	public void keyReleased(int key, char c) {
		if (key == Input.KEY_ESCAPE) {
			close();
		}
		
		if(focus != null)
			focus.keyReleased(key, c);
	}

	
	public void keyPressed(int key, char c, boolean repeated) {
		if(focus != null)
			focus.keyPressed(key, c, repeated);
	}
	
	public void mouseMoved(int oldx, int oldy, int newx, int newy) {
		for(Widget w: widgets) {
			w.mouseMoved(oldx, oldy, newx, newy);
		}
	}
	
	public void mouseDragged(int oldx, int oldy, int newx, int newy) {
		for(Widget w: widgets) {
			w.mouseDragged(oldx, oldy, newx, newy);
		}
	}

	public void mouseReleased(int button, int x, int y) {
		for(Widget w: widgets) {
			w.mouseReleased(button, x, y);
		}
	}
	
	public void mousePressed(int button, int x, int y) {
		for(Widget w: widgets) {
			w.mousePressed(button, x, y);
			if(w.contains(x, y)) {
				if(focus != null)
					focus.setFocus(false);
				focus = w;
				focus.setFocus(true);
			}
		}
	}
	
	public void activate() {
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public void mouseWheelMoved(int newValue) {
	}
}
